package com.lenovo.smarttraffic.zm.wei;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.zm.Wei.Api.Api_GetCarPeccancy;
import com.lenovo.smarttraffic.zm.Wei.Bean.Bean_GetCarPeccancy;
import com.lenovo.smarttraffic.zm.Wei.Bean_json.Bean_json_GetCarPeccancy;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author zm
 * @version 2.0
 * @date 2020/9/22 10:32
 */
public class CarPenaccyActivivty extends BaseActivity implements View.OnClickListener {
    private EditText et_carNumber;
    private Button btn_check;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected int getLayout() {
        return R.layout.carpenaccy_layout;
    }

    private void initView() {
        et_carNumber = (EditText) findViewById(R.id.et_carNumber);
        btn_check = (Button) findViewById(R.id.btn_check);
        btn_check.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_check:
                submit();
                httpPenaccy();
                break;
        }
    }

    private void httpPenaccy() {
//        GetCarPeccancy.do
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.110:8088/transportservice/action/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Api_GetCarPeccancy api_getCarPeccancy = retrofit.create(Api_GetCarPeccancy.class);
        Call<Bean_GetCarPeccancy> call = api_getCarPeccancy.getCall(new Bean_json_GetCarPeccancy("user1", "鲁" + et_carNumber.getText().toString()));
        call.enqueue(new Callback<Bean_GetCarPeccancy>() {
            @Override
            public void onResponse(Call<Bean_GetCarPeccancy> call, Response<Bean_GetCarPeccancy> response) {
                if (response.code() == 200) {
                    Intent intent = new Intent(CarPenaccyActivivty.this, ContentActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("CarBean", response.body());
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(CarPenaccyActivivty.this, "没有查询到" + et_carNumber.getText().toString() + "车的违章记录", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<Bean_GetCarPeccancy> call, Throwable t) {

            }
        });
    }

    private void submit() {
        // validate
        String carNumber = et_carNumber.getText().toString().trim();
        if (TextUtils.isEmpty(carNumber)) {
            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

    }
}
